<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/10
 * Time: 8:47
 */

namespace App\Http\Controllers\Admin;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\RoleModel;
use App\Http\Model\RolePermissionModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RoleController extends Controller
{
    /**
     * 角色列表
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request)
    {
        $rules = [
            'isProject' => 'nullable|integer',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:0',
        ];
        $message = [
            'isProject.integer' => '角色类型参数类型错误',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('isProject','length','page');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $roleModel = new RoleModel();
            $lists = $roleModel->lists($param);
            $countLists = $roleModel->countLists($param);
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 130101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 角色添加
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'name' => 'required',
            'isProject' => 'required|integer',
        ];
        $message = [
            'name.required' => '请填写角色名称',
            'isProject.required' => '请选择角色类型',
            'isProject.integer' => '角色类型参数类型错误',
        ];
        $param = $request->only(['name', 'isProject','remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $roleModel = new RoleModel();
            $result = $roleModel->checkRepeat(['name' => $param['name'], 'isProject' => $param['isProject']]);
            if (!empty($result)) {
                throw new ApiException('相同类型下已经存在相同名称的角色，请勿重复设置', 130202);
            }
            $roleModel->insert($param);
        } else {
            throw new ApiException($validator->errors()->first(), 130201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 角色详情
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取角色参数失败',
            'id.integer' => '角色参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $roleModel = new RoleModel();
            $this->data = $roleModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 130301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 角色编辑
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'isProject' => 'required|integer'
        ];
        $message = [
            'id.required' => '获取角色参数失败',
            'id.integer' => '角色参数类型错误',
            'name.required' => '请填写角色名称',
            'isProject.required' => '请选择角色类型',
            'isProject.integer' => '角色类型参数类型错误',
        ];
        $param = $request->only(['id', 'name', 'isProject', 'remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $roleModel = new RoleModel();
            $id = $param['id'];
            unset($param['id']);
            if ($id == 1){
                throw new ApiException('不能修改超级管理员角色', 130302);
            }
            $result = $roleModel->checkRepeat(['name'=>$param['name'],'isProject'=>$param['isProject']],$id);
            if (!empty($result)){
                throw new ApiException('已经存在相同名称的角色，请勿重复设置', 130303);
            }
            $roleModel->update(['id'=>$id], $param);
        } else {
            throw new ApiException($validator->errors()->first(), 130301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 角色状态更新
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editStatus(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'status' => 'required|integer|in:0,1',
        ];
        $message = [
            'id.required' => '获取角色参数失败',
            'id.integer' => '角色参数类型错误',
            'status.required' => '获取角色状态参数失败',
            'status.integer' => '角色状态参数类型错误',
            'status.in' => '角色状态参数不正确',
        ];
        $param = $request->only(['id', 'status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $roleModel = new RoleModel();
            $id = $param['id'];
            unset($param['id']);
            if($id == 1){
                throw new ApiException('不能修改超级管理员角色', 130402);
            }
            $roleModel->update(['id'=>$id], $param);
        } else {
            throw new ApiException($validator->errors()->first(), 130401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 角色删除
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delete(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取角色参数失败',
            'id.integer' => '角色参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $roleModel = new RoleModel();
            if($param['id'] == 1){
                throw new ApiException('不能删除超级管理员角色', 130502);
            }
            $result = $roleModel->delete($param);
            if (!$result){
                throw new ApiException('删除失败', 130503);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 130501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 保存角色的权限表
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function setPermission(Request $request){
        $rules = [
            'roleId' => 'required|integer',
            'permission' => 'array',
        ];
        $message = [
            'roleId.required' => '获取角色参数失败',
            'roleId.integer' => '角色参数类型错误',
            'permission.array' => '权限参数类型错误',
        ];
        $param = $request->only(['roleId','permission']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if($param['roleId'] == 1){
                throw new ApiException('不能设置超级管理员角色权限', 130602);
            }
            $rolePermissionModel = new RolePermissionModel();
            $result = $rolePermissionModel->insert($param);
            $this->msg = '成功设置 '.$result.' 条功能的权限';
        } else {
            throw new ApiException($validator->errors()->first(), 130601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function getPermission(Request $request){
        $rules = [
            'roleId' => 'required|integer',
        ];
        $message = [
            'roleId.required' => '获取角色参数失败',
            'roleId.integer' => '角色参数类型错误',
        ];
        $param = $request->only(['roleId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $rolePermissionModel = new RolePermissionModel();
            $this->data = $rolePermissionModel->lists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 130701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 角色下拉列表
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function selectLists(Request $request)
    {
        $rules = [
            'projectId' => 'nullable|integer',
        ];
        $message = [
            'projectId.integer' => '项目参数类型错误',
        ];
        $param = $request->only(['projectId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $roleModel = new RoleModel();
            $this->data = $roleModel->selectLists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 130801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}